package com.dyh.quickdemozwei.withdraw.biz;

import com.dyh.quickdemozwei.withdraw.Action;
import com.dyh.quickdemozwei.withdraw.ActionPlan;
import com.dyh.quickdemozwei.withdraw.ActionWrap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yhdong
 * @Date: 2021/11/30 16:34
 */
@Configuration
public class ProductActionConfig {

    @Autowired
    ProductBiz productBiz;

    @Bean
    ActionPlan publishPlan(){
        ActionPlan actionPlan = new ActionPlan();

        List<ActionWrap<ProductBiz.FullNagareContext>> actions = new ArrayList<>();
        actions.add(ActionWrap.bind(
            (e,ctrl)-> {
                boolean res =productBiz.put(e.masterProd.productId,e.masterProd.productBoby);
                if (!res)
                    ctrl.setShouldWithDraw(true);
            },
            (e,ctrl)-> {
                productBiz.delete(e.masterProd.productId);
            }
        ));

        actions.add(ActionWrap.from((e,ctrl)-> {
            boolean res =productBiz.putAttr(e.putAttrContext.attrId,e.putAttrContext.attrBody);
            if (!res)
                ctrl.setShouldWithDraw(true);

//            throw new RuntimeException();
        }));


        actions.add(ActionWrap.from((e,ctrl)-> {
//            boolean res =productBiz.put(e.subProd.productId,e.subProd.productBoby);
//            if (!res)
//                ctrl.setShouldWithDraw(true);

//            throw new RuntimeException();
            ctrl.setShouldWithDraw(true);

        }));

        actionPlan.setActions(actions);
        return actionPlan;

//        actions.add(e-> put(e.masterProd.productId,e.masterProd.productBoby));
//        actions.add(e-> putAttr(e.putAttrContext.attrId,e.putAttrContext.attrBody));
//        actionPlan.add(e->
//                        productBiz.put(e)
//                );
    }

}
